package cn.edu.scau.cmi.ema.dao;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.domain.Crawlerfile;
import cn.edu.scau.cmi.ema.util.CmiSqlUtil;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.*;

@Repository("CrawlerfileDAO")
@Transactional
public class CrawlerfileDAOImpl extends AbstractJpaDao<Crawlerfile> implements CrawlerfileDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Crawlerfile.class }));

	@PersistenceContext()
	private EntityManager entityManager;

	public CrawlerfileDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Crawlerfile findCrawlerfileById(Integer id) throws DataAccessException {

		return findCrawlerfileById(id, -1, -1);
	}

	@Transactional
	public Crawlerfile findCrawlerfileById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findCrawlerfileById", startResult, maxRows, id);
			return (Crawlerfile) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Crawlerfile> findCrawlerfileByName(String name) throws DataAccessException {

		return findCrawlerfileByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawlerfile> findCrawlerfileByName(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findCrawlerfileByName", startResult, maxRows, name);
		return new LinkedHashSet<Crawlerfile>(query.getResultList());
	}

	@Transactional
	public Set<Crawlerfile> findCrawlerfileByNameContaining(String name) throws DataAccessException {

		return findCrawlerfileByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawlerfile> findCrawlerfileByNameContaining(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findCrawlerfileByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Crawlerfile>(query.getResultList());
	}

	@Transactional
	public Set<Crawlerfile> findCrawlerfileByCrawltime(Calendar crawltime) throws DataAccessException {

		return findCrawlerfileByCrawltime(crawltime, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawlerfile> findCrawlerfileByCrawltime(Calendar crawltime, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findCrawlerfileByCrawltime", startResult, maxRows, crawltime);
		return new LinkedHashSet<Crawlerfile>(query.getResultList());
	}

	@Transactional
	public Set<Crawlerfile> findCrawlerfileByExist(Boolean exist) throws DataAccessException {

		return findCrawlerfileByExist(exist, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawlerfile> findCrawlerfileByExist(Boolean exist, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findCrawlerfileByExist", startResult, maxRows, exist);
		return new LinkedHashSet<Crawlerfile>(query.getResultList());
	}

	@Transactional
	public Set<Crawlerfile> findCrawlerfileByTitlerownum(Integer titlerownum) throws DataAccessException {

		return findCrawlerfileByTitlerownum(titlerownum, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawlerfile> findCrawlerfileByTitlerownum(Integer titlerownum, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findCrawlerfileByTitlerownum", startResult, maxRows, titlerownum);
		return new LinkedHashSet<Crawlerfile>(query.getResultList());
	}

	@Transactional
	public Set<Crawlerfile> findCrawlerfileByHash(Integer hash) throws DataAccessException {

		return findCrawlerfileByHash(hash, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawlerfile> findCrawlerfileByHash(Integer hash, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findCrawlerfileByHash", startResult, maxRows, hash);
		return new LinkedHashSet<Crawlerfile>(query.getResultList());
	}

	@Transactional
	public Set<Crawlerfile> findCrawlerfileByDescription(String description) throws DataAccessException {

		return findCrawlerfileByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawlerfile> findCrawlerfileByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findCrawlerfileByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Crawlerfile>(query.getResultList());
	}

	@Transactional
	public Set<Crawlerfile> findCrawlerfileByDescriptionContaining(String description) throws DataAccessException {

		return findCrawlerfileByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawlerfile> findCrawlerfileByDescriptionContaining(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findCrawlerfileByDescriptionContaining", startResult, maxRows, description);
		return new LinkedHashSet<Crawlerfile>(query.getResultList());
	}

	@Transactional
	public Crawlerfile findCrawlerfileByPrimaryKey(Integer id) throws DataAccessException {
		return findCrawlerfileByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Crawlerfile findCrawlerfileByPrimaryKey(Integer id, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findCrawlerfileByPrimaryKey", startResult, maxRows, id);
			return (Crawlerfile) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Crawlerfile> findCrawlerfilesByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findCrawlerfilesByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Crawlerfile> findAllCrawlerfiles() throws DataAccessException {

		return findAllCrawlerfiles(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawlerfile> findAllCrawlerfiles(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllCrawlerfiles", startResult, maxRows);
		return new LinkedHashSet<Crawlerfile>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Crawlerfile> findCrawlerfilesByUrl(Integer urlId, int start, int max) throws DataAccessException {
		Query query = createNamedQuery("findAllCrawlerfilesByUrl", start, max, urlId);
		return new LinkedHashSet<Crawlerfile>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Crawlerfile> findCrawlerfilesByFiletype(Integer filetypeId, int start, int max)
			throws DataAccessException {
		Query query = createNamedQuery("findAllCrawlerfilesByFiletype", start, max, filetypeId);
		return new LinkedHashSet<Crawlerfile>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Crawlerfile> findCrawlerfilesByCrawlerfilestatus(Integer crawlerfilestatusId, int start, int max)
			throws DataAccessException {
		Query query = createNamedQuery("findAllCrawlerfilesByCrawlerfilestatus", start, max, crawlerfilestatusId);
		return new LinkedHashSet<Crawlerfile>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Crawlerfile> findCrawlerfilesByCrawlerfilestandard(Integer crawlerfilestandardId, int start, int max)
			throws DataAccessException {
		Query query = createNamedQuery("findAllCrawlerfilesByCrawlerfilestandard", start, max, crawlerfilestandardId);
		return new LinkedHashSet<Crawlerfile>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Crawlerfile> findCrawlerfileByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Crawlerfile>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Crawlerfile> findCrawlerfilesByDetachedCriteria(CmiPagination page) throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Crawlerfile.class);
		Session session = (Session) entityManager.getDelegate();
		Set<Crawlerfile> crawlerfiles = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return crawlerfiles;
	}

	@Transactional
	public Crawlerfile findCrawlerfileByPhysicalPrimaryKey(String name) throws DataAccessException {
		return findCrawlerfileByPhysicalPrimaryKey(name, -1, -1);
	}

	@Transactional
	public Crawlerfile findCrawlerfileByPhysicalPrimaryKey(String name, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findCrawlerfileByPhyscialPrimaryKey", startResult, maxRows, name);
			return (Crawlerfile) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Crawlerfile entity) {
		return true;
	}
}
